//
//  Car_CollectionTableViewCell.m
//  CarUser
//
//  Created by ayi on 2019/1/10.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "Car_CollectionTableViewCell.h"

@interface Car_CollectionTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *childCollectionview;

@end

@implementation Car_CollectionTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

+ (Car_CollectionTableViewCell *)initWithReuseIdentifier:(NSString *)reuseIdentifier andTab:(UITableView *)tab{
    Car_CollectionTableViewCell *cell = [tab dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[Car_CollectionTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        @weakify(self);
        _childCollectionview = ({
            @strongify(self);
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
            layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
            UICollectionView *collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
            collection.delegate = self;
            collection.dataSource = self;
            collection.backgroundColor = UIColor.clearColor;
            collection.showsVerticalScrollIndicator = NO;
            collection.showsHorizontalScrollIndicator = NO;
            
            [collection registerClass:[Car_ChildCollectionCollectionCell class] forCellWithReuseIdentifier:@"Car_ChildCollectionCollectionCell"];
            
            collection;
        });
        
        [self.contentView addSubview:_childCollectionview];
        
        [_childCollectionview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            (void)make.left.and.right;
            make.height.mas_equalTo(180);
            make.bottom.mas_equalTo(-20);
        }];
    }
    return self;
}

- (void)setListAry:(NSMutableArray *)listAry{
    _listAry = listAry;
    
    [self.childCollectionview reloadData];
}




#pragma mark --------------- UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listAry.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(140, 200);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Car_ChildCollectionCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Car_ChildCollectionCollectionCell" forIndexPath:indexPath];
    cell.model = self.listAry[indexPath.row];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CarSaleCarListModel *model = self.listAry[indexPath.row];
    if (self.collectionselectedDelegate && [self.collectionselectedDelegate respondsToSelector:@selector(vm_collectionSelected:)]) {
        [self.collectionselectedDelegate vm_collectionSelected:model];
    }
}



@end






@interface Car_ChildCollectionCollectionCell()

@property (nonatomic, strong) UIImageView *imgv;
@property (nonatomic, strong) UILabel *titleLab;
//@property (nonatomic, strong) UILabel *tipsLab;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *typeLabel1;
@property (nonatomic, strong) UILabel *moneyLab;

@end

@implementation Car_ChildCollectionCollectionCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        _imgv = [[UIImageView alloc]init];
        _titleLab = [UILabel customInitLabWithFont:14 andTextColor:RGB(40, 40, 40) andAlign:NSTextAlignmentLeft];
        _titleLab.numberOfLines = 2;
//        _tipsLab = [UILabel customInitLabWithFont:12 andTextColor:Common_TextLowColor andAlign:NSTextAlignmentLeft];
        _moneyLab = [UILabel customInitLabWithFont:18 andTextColor:RGB(49, 49, 49) andAlign:NSTextAlignmentLeft];
        _moneyLab.font = B_FONT_SIZE(18);
        
        [self.contentView addSubview:_imgv];
        [self.contentView addSubview:_titleLab];
        [self.contentView addSubview:self.typeLabel];
         [self.contentView addSubview:self.typeLabel1];
        [self.contentView addSubview:_moneyLab];
        
        [_imgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            (void)make.left.and.right;
            make.height.mas_equalTo(84);
        }];
        
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imgv.mas_bottom).offset(5);
            (void)make.left.and.right;
            make.height.mas_lessThanOrEqualTo(ceilf(self.titleLab.font.lineHeight) * 2);
        }];
        
//        [_tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.imgv.mas_bottom).offset(ceilf(self.titleLab.font.lineHeight) * 2 + 11);
//            (void)make.left.and.right;
//        }];
//        [_tipsLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        
        
        [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-40);
            make.left.equalTo(self.contentView.mas_left).offset(0);
            make.width.mas_equalTo(38);
            make.height.mas_equalTo(18);
        }];
        
        [self.typeLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-40);
            make.left.equalTo(self.typeLabel.mas_right).offset(10);
            make.width.mas_equalTo(38);
            make.height.mas_equalTo(18);
        }];
        
        
        [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.typeLabel.mas_bottom).offset(4);
           (void)make.left.and.right;
            make.height.mas_equalTo(ceilf(self.moneyLab.font.lineHeight));
        }];
        
        
    
        
    }
    return self;
}


- (void)setModel:(CarSaleCarListModel *)model{
    _model = model;
    
    @weakify(self);
    [self.imgv sd_setImageWithURL:[NSURL URLWithString:model.ZST000] placeholderImage:[UIImage getDefaultImage] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        @strongify(self);
        if (image && cacheType == SDImageCacheTypeNone) {
            [self.imgv.layer addCustomImageViewLayerAnimation];
        }
    }];
    
    self.titleLab.text = model.CXMC00;
    //标签
    [self resetTipsLabText];
    //价格
    self.moneyLab.text = [NSString stringWithFormat:@"￥%@万",model.ZDJG00];
}

//标签
- (void)resetTipsLabText{
    
    self.typeLabel.text = self.model.GGBBMC?:@"";
    self.typeLabel1.text = self.model.GHFSMC?:@"";
    
//    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]init];
//    [str appendAttributedString:[UIView imgAttirbuteStrWith:self.model.GGBBMC?:@""]];
//    [str appendAttributedString:[[NSMutableAttributedString alloc]initWithString:@"   "]];
//    [str appendAttributedString:[UIView imgAttirbuteStrWith:self.model.GHFSMC?:@""]];
//
//    self.tipsLab.attributedText = str;
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [UILabel new];
        _typeLabel.textColor = Common_Text_TextfieldColor;
        _typeLabel.textAlignment = NSTextAlignmentCenter;
        _typeLabel.font = FONT_SIZE(12);
        _typeLabel.layer.borderWidth = 1;
        _typeLabel.layer.cornerRadius = 6;
        _typeLabel.layer.masksToBounds = YES;
        _typeLabel.layer.borderColor = Common_Head_Color.CGColor;
    }
    return _typeLabel;
    
}

- (UILabel *)typeLabel1
{
    if (!_typeLabel1) {
        _typeLabel1 = [UILabel new];
        _typeLabel1.textColor = Common_Text_TextfieldColor;
        _typeLabel1.textAlignment = NSTextAlignmentCenter;
        _typeLabel1.font = FONT_SIZE(12);
        _typeLabel1.layer.borderWidth = 1;
        _typeLabel1.layer.cornerRadius = 6;
        _typeLabel1.layer.masksToBounds = YES;
        _typeLabel1.layer.borderColor = Common_Head_Color.CGColor;
    }
    return _typeLabel1;
    
}

@end
